home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / dllole.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  8.2 KB  |  290 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Initialization of MFC extension DLL
  24.  
  25. static AFX_EXTENSION_MODULE extensionDLL;
  26. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState();
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Library initialization and cleanup
  30.  
  31. extern "C" BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID);
  32. extern "C" BOOL (WINAPI* _pRawDllMain)(HINSTANCE, DWORD, LPVOID) = &RawDllMain;
  33.  
  34. #if defined(_WIN32_WCE)
  35. #ifdef _DEBUG
  36. #define MFC42_DLL WCE_MFC_FILENAME(D.DLL)
  37. #else
  38. #define MFC42_DLL WCE_MFC_FILENAME(.DLL)
  39. #endif
  40. #else // _WIN32_WCE 
  41. #ifdef _DEBUG
  42. #ifndef _UNICODE
  43. #define MFC42_DLL "MFC42D.DLL"
  44. #else
  45. #define MFC42_DLL "MFC42UD.DLL"
  46. #endif
  47. #else
  48. #ifndef _UNICODE
  49. #define MFC42_DLL "MFC42.DLL"
  50. #else
  51. #define MFC42_DLL "MFC42U.DLL"
  52. #endif
  53. #endif
  54. #endif // _WIN32_WCE
  55.  
  56. extern "C"
  57. BOOL WINAPI RawDllMain(HINSTANCE, DWORD dwReason, LPVOID)
  58. {
  59.     if (dwReason == DLL_PROCESS_ATTACH)
  60.     {
  61.         // Prevent the MFC DLL from being unloaded prematurely
  62.         WCE_FCTN(LoadLibraryA)(MFC42_DLL);
  63.  
  64.         // make sure we have enough memory to attempt to start (8kb)
  65.         void* pMinHeap = LocalAlloc(NONZEROLPTR, 0x2000);
  66.         if (pMinHeap == NULL)
  67.             return FALSE;   // fail if memory alloc fails
  68.         LocalFree(pMinHeap);
  69.  
  70.         // save critical data pointers before running the constructors
  71.         AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
  72.         pModuleState->m_pClassInit = pModuleState->m_classList;
  73.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList;
  74.         pModuleState->m_classList.m_pHead = NULL;
  75.         pModuleState->m_factoryList.m_pHead = NULL;
  76.  
  77.         // set module state before initialization
  78.         pModuleState = _AfxGetOleModuleState();
  79.         _AFX_THREAD_STATE* pState = AfxGetThreadState();
  80.         pState->m_pPrevModuleState = AfxSetModuleState(pModuleState);
  81.     }
  82.     else if (dwReason == DLL_PROCESS_DETACH)
  83.     {
  84.         // restore previously-saved module state
  85.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  86.             _AfxGetOleModuleState());
  87.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  88.  
  89.         // Now it's OK for the MFC  DLL to be unloaded (see LoadLibrary above)
  90.         FreeLibrary(WCE_FCTN(GetModuleHandleA)(MFC42_DLL));
  91.     }
  92.     return TRUE;    // ok
  93. }
  94.  
  95. extern "C"
  96. #if defined(_WIN32_WCE)
  97. BOOL WINAPI DllMain(HANDLE wce_hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  98. {
  99.     HINSTANCE hInstance = (HINSTANCE)wce_hInstance;
  100. #else // _WIN32_WCE
  101. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  102. {
  103. #endif // _WIN32_WCE
  104.     if (dwReason == DLL_PROCESS_ATTACH)
  105.     {
  106.         // shared initialization
  107.         BOOL bRegister = !extensionDLL.bInitialized;
  108.         AFX_MODULE_STATE* pModuleState = _AfxGetOleModuleState();
  109.         pModuleState->m_hCurrentInstanceHandle = hInstance;
  110.         pModuleState->m_hCurrentResourceHandle = hInstance;
  111.         pModuleState->m_pClassInit = pModuleState->m_classList.GetHead();
  112.         pModuleState->m_pFactoryInit = pModuleState->m_factoryList.GetHead();
  113.         VERIFY(AfxInitExtensionModule(extensionDLL, hInstance));
  114.  
  115.         // wire up base DLL class list into private module state
  116.         AfxCoreInitModule();
  117.  
  118.         AfxWinInit(hInstance, NULL, _T(""), 0);
  119.  
  120.         // Register class factories in context of private module state
  121.         if (bRegister)
  122.             COleObjectFactory::RegisterAll();
  123.  
  124.         // restore previously-saved module state
  125.         VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) ==
  126.             _AfxGetOleModuleState());
  127.         DEBUG_ONLY(AfxGetThreadState()->m_pPrevModuleState = NULL);
  128.  
  129.         // wire up this DLL into the base module state resource chain
  130.         CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  131.         ASSERT(pDLL != NULL);
  132.         pDLL->m_factoryList.m_pHead = NULL;
  133.     }
  134.     else if (dwReason == DLL_PROCESS_DETACH)
  135.     {
  136. #if defined(_WIN32_WCE)
  137.         wce_UnregisterOleWindowClasses();
  138. #endif // _WIN32_WCE
  139.         // cleanup module state in base module state
  140.         AfxTermExtensionModule(extensionDLL);
  141.  
  142.         // set module state for cleanup
  143.         ASSERT(AfxGetThreadState()->m_pPrevModuleState == NULL);
  144.         AfxGetThreadState()->m_pPrevModuleState =
  145.             AfxSetModuleState(_AfxGetOleModuleState());
  146.  
  147.         // cleanup module state in OLE private module state
  148.         AfxTermExtensionModule(extensionDLL, TRUE);
  149.     }
  150.     else if (dwReason == DLL_THREAD_DETACH)
  151.     {
  152.         AfxTermThread(hInstance);
  153.     }
  154.  
  155.     return TRUE;    // ok
  156. }
  157.  
  158. ////////////////////////////////////////////////////////////////////////////
  159. // Special initialization entry point for controls
  160.  
  161. void AFXAPI AfxOleInitModule()
  162. {
  163.     ASSERT(AfxGetModuleState() != AfxGetAppModuleState());
  164.  
  165.     CDynLinkLibrary* pDLL = new CDynLinkLibrary(extensionDLL, TRUE);
  166.     ASSERT(pDLL != NULL);
  167.     pDLL->m_factoryList.m_pHead = NULL;
  168. }
  169.  
  170. ////////////////////////////////////////////////////////////////////////////
  171. // COM entry points
  172.  
  173. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  174. {
  175.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  176.     return AfxDllGetClassObject(rclsid, riid, ppv);
  177. }
  178.  
  179. STDAPI DllCanUnloadNow(void)
  180. {
  181.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  182.     return AfxDllCanUnloadNow();
  183. }
  184.  
  185. ////////////////////////////////////////////////////////////////////////////
  186. // Server registration
  187.  
  188. STDAPI DllRegisterServer(void)
  189. {
  190.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  191.  
  192.     if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  193.         return SELFREG_E_CLASS;
  194.  
  195.     return S_OK;
  196. }
  197.  
  198. STDAPI DllUnregisterServer(void)
  199. {
  200.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  201.  
  202.     if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
  203.         return SELFREG_E_CLASS;
  204.  
  205.     return S_OK;
  206. }
  207.  
  208. // This CWinApp is required so this module state has a CWinApp object!
  209. CWinApp _afxOleWinApp;
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // static-linked version of AfxWndProc for use by this module
  213.  
  214. #undef AfxWndProc
  215.  
  216. LRESULT CALLBACK
  217. AfxWndProcDllOle(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  218. {
  219.     AFX_MANAGE_STATE(_AfxGetOleModuleState());
  220.     return AfxWndProc(hWnd, nMsg, wParam, lParam);
  221. }
  222.  
  223. /////////////////////////////////////////////////////////////////////////////
  224. // initialize app state such that it points to this module's core state
  225.  
  226. #ifndef _AFX_NO_OCC_SUPPORT
  227. class _AFX_INIT_OLE_DLL_STATE
  228. {
  229. public:
  230.     ~_AFX_INIT_OLE_DLL_STATE();
  231. };
  232.  
  233. _AFX_INIT_OLE_DLL_STATE::~_AFX_INIT_OLE_DLL_STATE()
  234. {
  235.     // OLE dll is not loaded any more, so no OLE controls capability
  236.     afxOccManager = NULL;
  237. }
  238. #endif
  239.  
  240. /////////////////////////////////////////////////////////////////////////////
  241.  
  242. // force initialization early
  243. #pragma warning(disable: 4074)
  244. #pragma init_seg(lib)
  245.  
  246. static AFX_MODULE_STATE _afxOleModuleState(TRUE, &AfxWndProcDllOle,
  247.     _MFC_VER, TRUE);
  248. #ifndef _AFX_NO_OCC_SUPPORT
  249. _AFX_INIT_OLE_DLL_STATE _afxInitOleDllState;
  250. #endif
  251.  
  252. AFX_MODULE_STATE* AFXAPI _AfxGetOleModuleState()
  253. {
  254.     return &_afxOleModuleState;
  255. }
  256.  
  257. /////////////////////////////////////////////////////////////////////////////
  258.  
  259. #ifdef AFX_VDEL_SEG
  260. #pragma code_seg(AFX_VDEL_SEG)
  261. #endif
  262. static void _AfxForceVectorDelete()
  263. {
  264.     ASSERT(FALSE);  // never called
  265.  
  266.     new COleDataSource[2];
  267.     new COleDispatchDriver[2];
  268. WCE_DEL new COleDropSource[2];
  269. WCE_DEL new COleResizeBar[2];
  270.     new COleStreamFile[2];
  271. WCE_DEL new CMonikerFile[2];
  272. WCE_DEL new COleTemplateServer[2];
  273. WCE_DEL new COleDataObject[2];
  274. WCE_DEL new COleDropTarget[2];
  275. WCE_DEL new COleIPFrameWnd[2];
  276.  
  277. WCE_DEL new COleDocIPFrameWnd[2];
  278. WCE_DEL new CAsyncMonikerFile[2];
  279. WCE_DEL new CCachedDataPathProperty[2];
  280. WCE_DEL new CDataPathProperty[2];
  281.  
  282.     new COleVariant[2];
  283.  
  284. WCE_DEL new CRichEditView[2];
  285. WCE_DEL new CRichEditCntrItem[2];
  286. }
  287. void (*_afxForceVectorDelete_mfco)() = &_AfxForceVectorDelete;
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290.